import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import path from 'path'
import autoprefixer from 'autoprefixer'
import postCssPxToRem from 'postcss-pxtorem'
import basicSsl from '@vitejs/plugin-basic-ssl'
import { terser } from 'rollup-plugin-terser'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react(), basicSsl()],
  server: {
    host: '0.0.0.0',
    open: true,
    https: true,
  },

  define: {
    // 配置获取环境变量
    'process.env': process.env,
  },
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: '@import "./src/assets/style/color.scss";',
        javascriptEnabled: true,
      },
    },
    postcss: {
      plugins: [
        autoprefixer({
          overrideBrowserslist: [
            'Android 4.1',
            'iOS 7.1',
            'Chrome > 31',
            'ff > 31',
            'ie >= 8',
          ],
        }),
        postCssPxToRem({
          // 自适应，px>rem转换
          rootValue: 192, // 75表示750设计稿，37.5表示375设计稿
          propList: ['*'], // 需要转换的属性，这里选择全部都进行转换
          mediaQuery: true, // 媒体查询里的单位是否需要转换单位
          replace: true, //  是否直接更换属性值，而不添加备用属性
        }),
      ],
    },
  },

  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },

  build: {
    target: 'es2020',
    minify: 'terser',
    terserOptions: {
      compress: {
        drop_console: true,
        drop_debugger: true,
        booleans: true,
        dead_code: true,
        directives: true,
      },
    },
    rollupOptions: {
      plugins: [
        terser({
          format: {
            // 取消代码注释
            comments: false,
          },

          mangle: {
            keep_classnames: false,
            reserved: [],
          },
        }),
      ],

      // make sure to externalize deps that shouldn't be bundled
      // into your library
      external: ['some_amazing_package_I_installed'],
      // external: ['konva'],
      output: {
        // Provide global variables to use in the UMD build
        // for externalized deps
        globals: {},
      },
    },
  },
})
